#include <stdio.h>
#include <sys/types.h>//open
#include <sys/stat.h>//open
#include <fcntl.h>//open
#include <unistd.h>//close
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    //日期、温度、湿度数据，通过C语言数据类型去写数据
    char experimental_field_A2[2][1024]={
        "20231212-28.1-66",  //experimental_field_A2[0]
        "20231211-26.1-40"     //experimental_field_A2[1]
    };

    char experimental_field_A3[2][1024]={ 0 };  

    //打开开发板1.txt文件
    const char *pathname = "1.txt";
    int flags =  O_RDWR;

    int fd_txt = -1;
    fd_txt = open(pathname, flags);
    //返回值有成功(文件描述符)和失败(-1)
    if(-1 == fd_txt)
    {
        printf("打开%s文件失败\n", pathname);
        return -1;
    }else{
        //成功就打印一下变量的值
        printf("打开%s文件成功\n", pathname);
        printf("开发板硬盘中1.txt对应的文件描述符是%d\n", fd_txt);
    }

    int ret = -1;
    ret = write(fd_txt, &experimental_field_A2[0], 1024);
    if(-1 == ret)
    {
        printf("写入数据失败!\n");
        return -1;
    }else{
        printf("成功写入%d到1.txt文本中\n", ret);
    }


    ret = -1;
    ret = write(fd_txt, &experimental_field_A2[1], 1024);
    if(-1 == ret)
    {
        printf("写入数据失败!\n");
        return -1;
    }else{
        printf("成功写入%d到1.txt文本中\n", ret);
    }

    lseek(fd_txt, 0, SEEK_SET);//配置光标的位置到文件的开头


    ret = -1;
    ret = read(fd_txt, &experimental_field_A3[0], 1024);
    if(-1 == ret)
    {
        printf("读取数据失败\n");
        return -1;
    }else{
        printf("从开发板GEC6818中成功读取%d个字节\n", ret);
        printf("读取的数据%s\n", experimental_field_A3[0]);
    }


    //关闭1.txt文件
    close(fd_txt);
    return 0;
}